package org.shan.lis.utils;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by amanda.shan on 2019-07-06.
 */
public class LisDataUtils {

    public static String[] split(byte[] bytes, int len) {

        List<String> list = new ArrayList<>();

        int matchPos = 0;

        for (int i = 0; i < len; i++) {
            if (bytes[i] == '|') {
                if (i - matchPos == 0) {
                    list.add(null);
                } else {
                    list.add(new String(bytes, matchPos, i - matchPos, Charset.forName("ASCII")));
                }

                matchPos = i + 1;
            }
        }

        if (matchPos < len) {
            list.add(new String(bytes, matchPos, len - matchPos, Charset.forName("ASCII")));
        }

        return list.toArray(new String[0]);
    }

    public static void main(String[] args) {

        byte[] bytes = "O|1|001||^^^12|||20190510000000||0|||||||02".getBytes();

        String[] strings = split(bytes, bytes.length);
        System.out.println();
    }

}
